//
// Created by (cie_tea)15694 on 2024/5/23.
//

#ifndef TCP_GAME_SERVER_IOBUFFER_H
#define TCP_GAME_SERVER_IOBUFFER_H


#include <cstdlib>
#include <string>
#include <cstring>
#include <stdint.h>
#include <iostream>


typedef unsigned char byte;
typedef unsigned char BYTE;


class IoBuffer {
public:
    BYTE  *byteStream;
    int capacity;
    int position,mark,limit;
    //limit 读写模式下为 可操作的最后一个
    IoBuffer(){
        //初始状态为写状态
	    int size;
        size = 1024 * 1024;
        byteStream = static_cast<BYTE  *>(malloc(sizeof(BYTE ) * size));
        memset(byteStream,'\0',size);
        position = 0;
        mark = -1;
        capacity = size-1;
        limit = capacity;
    }
    explicit IoBuffer(int size){
        //初始状态为写状态
        byteStream = static_cast<BYTE  *>(malloc(sizeof(BYTE ) * size));
        memset(byteStream,'\0',size);
        position = 0;
        mark = -1;
        capacity = size-1;
        limit = capacity;
    }
    ~IoBuffer(){
        free(byteStream);
    }
    //position指向数据首部，设置清空
    IoBuffer* clear(){
        position = 0;
        limit = capacity;
        mark = -1;
        return this;
    }
    //设置读模式，position指向数据首部，并且limit指向数据尾部
    IoBuffer* filp(){
        if(limit != capacity) return this;
        limit = position - 1;
        position = 0;
        mark = -1;
        return this;
    }
    //postion指向缓冲区的数据末尾，并设置为写模式,读取完之后做一个压缩
    IoBuffer* compact(){
        int remainLen = limit - position + 1;
        std::cout<< "remain_len=>" << remainLen<<std::endl;
        memcpy(byteStream,byteStream + position,remainLen);
        position = remainLen;
        limit = capacity;
        mark = -1;
        return this;
    }
    //记录当前位置
    void markPos(){
        mark = position;
    }
    //还原当前位置
    void remarkPos(){
        if(-1 == mark) return;
        position = mark;
    }
    /*BYTE * getBufferByte(int len){
        BYTE  *bytes = new BYTE [len+1];
        memcpy(bytes,byteStream+position,len);
        position+=len;
        return bytes;
    }*/
    std::string getString(int len){
        std::cout<<"no why ..."<<std::endl;
        std::string str((char*)byteStream+position,len);
        position+=len;
        str += '\0'; // 追加一个空字符
        //str.Format(_T("%s"),len);
        std::cout<<"str:"<<str<<std::endl;
        return str;
    }
    //传递给指定地址数据
    int getBuffer(BYTE  *p, int len){
        memcpy(p,byteStream+position,len);
        p[len] = '\0';
        position+=len;
        return len;
    }
    //写入值1
    int writeBuffer(BYTE  *p){
        int len = sizeof(p)/sizeof(BYTE );
        std::cout<<"len:"<<len;
        memcpy(byteStream+position,p,len);
        position+=len;
        return len;
    }

    // 写入值2
    int writeBuffer(BYTE *p, int len){
       size_t   byteStreamSize = 1024*1024;

        std::cout<< "byteStream is not allocated"<<std::endl;
        fprintf(stderr, "byteStream is not allocated.\n");

        // 确保 byteStream 已经分配
        if (byteStream == NULL) {
            fprintf(stderr, "byteStream is not allocated.\n");
            return -1;
        }

        // 确保 p 指针有效
        if (p == NULL) {
            fprintf(stderr, "Input buffer is NULL.\n");
            return -1;
        }

        // 检查 position 和 len 是否会导致越界写入
        if (position + len > byteStreamSize) {
            fprintf(stderr, "Attempt to write beyond the buffer. Position: %d, Length: %d, Buffer Size: %zu.\n", position, len, byteStreamSize);
            return -1;
        }

        // 执行复制
        std::cout<< "position:"<<position<<" len:"<<len<<std::endl;
        std::cout << std::hex << static_cast<unsigned int>((byteStream + position)[0]) << std::endl;
        std::cout<< "p"<<p[0]<<" len:"<<len<<std::endl;
        std::cout << std::hex << static_cast<unsigned int>((byteStream + position + len)[0]) << std::endl;
        std::cout<< "p len - 1: "<<p[10]<<" len:"<<len<<std::endl;
        std::cout<< "p len - 1: "<<p[len-10]<<" len:"<<len<<std::endl;
        std::cout<< "p"<<p[len]<<" len:"<<len<<std::endl;
        memcpy(byteStream + position, p, len);
        position += len;

        return len;
    }

    //从指定位置开始写入
    BYTE * getWriteIndexP(){
        return byteStream+position;
    }
    //从指定位置开始写入
    BYTE * getReadIndexP(){
        return byteStream+position;
    }
    //获得buff的数据长度
    int getBufferLen(){
        if(limit == capacity){
            //写模式
            return position;
        }else{
            //读模式
            return limit - position + 1;
        }
    }
    //获得buff的剩余长度
    int getRemainLen(){
	    // std::cout<<"iiiiiiiciiiii"<<std::endl;
         //
         //    if (std::cout.fail()) {
         //        std::cout.clear(); // 尝试清除错误状态
         //        std::cout << "Error occurred!" << std::endl;
         //    }
         //    std::cout << 123 << std::endl;
         //
	        // std::cout<<123<<std::endl;
	        // std::cout<<limit<<std::endl;
	        // std::cout<<position<<std::endl;
         //    std::cout<<"iixxiiiiiiii"<<std::endl;

	    std::cout<<"limit - position + 1 =>"<<limit - position + 1<<std::endl;
        return limit - position + 1;
        /*
        if(limit == capacity){
            //写模式
            return limit - position + 1;
        }else{
            //读模式
            return position+1;
        }*/
    }

    //获得和获得
    BYTE  getByte(){
        if(limit - position + 1>=1){
            BYTE  byte1 = *(byteStream+position);
            position++;
            return byte1;
        }else
            return 0;
    };
    void setByte(BYTE  byte1){
        *(byteStream+position) = byte1;
        position++;
        return;
    };
    short getShort(){
        if(limit - position + 1>=2){
            short len =  *(byteStream+position);
            position+=2;
            return len;
        }else
            return 0;
    };
    short setShort(){
        return (short)1;
    };
    int getInt(){
//        std::cout<<"why?"<<std::endl;
        int value;
        value = (int) (((*(byteStream+position+3) & 0xFF))
                       | ((*(byteStream+position+2) & 0xFF)<<8)
                       | ((*(byteStream+position+1) & 0xFF)<<16)
                       | ((*(byteStream+position) & 0xFF)<<24));
        position+=4;
        return value;
    };

    int setInt(int value){
        *(byteStream+position+3) = (byte) (value & 0xFF);
        *(byteStream+position+2) = (byte) ((value>>8) & 0xFF);
        *(byteStream+position+1) = (byte) ((value>>16) & 0xFF);
        *(byteStream+position) = (byte) ((value>>24) & 0xFF);
        position+=4;
        return 0;
    };

    /*float getFloat(){
        if(limit-position + 1>=4){
            position+=8;
            return (float)(byteStream+position);
        }else
            return 0;
    };
    float setFloat(){};*/
    long getLong(){
        if(limit-position + 1>=8){
            position+=8;
            return (long)(byteStream+position);
        }else
            return 0;
    };
    long setLong(){
        return (long)1;
    };
    double getDouble(){
        return (double)1;
    };
    double setDouble(){
        return (double)1;
    };
    bool getBool(){
        return (bool)1;
    };
    bool setBool(){
        return (bool)1;
    };



};


#endif //TCP_GAME_SERVER_IOBUFFER_H
